<!doctype html>



  


<html class="theme-next mist use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="baidu-site-verification" content="zD47G9xe42" />


<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="PHP核心编程," />





  <link rel="alternate" href="/atom.xml" title="uiste" type="application/atom+xml" />




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.1" />






<meta name="description" content="复习：匿名函数use用法：将外部和匿名函数处于同一级的变量引用到匿名函数的内部，引用的变量和外部的变量是一个值传递的关系。如果需要引用传址，则在usb变量前加&amp;amp;符号

迭代
单单从思想的角度上看，递归和迭代没有谁优谁劣，都是一样的，就和大家偏文科偏理科一样，但是，从代码的角度上来看，无疑是迭代的思想更加具有效率，所以建议大家以后去多用迭代的思想去解决问题。

数组不是所有关联数组都是字符">
<meta property="og:type" content="article">
<meta property="og:title" content="Day5-php数组相关">
<meta property="og:url" content="http://blog.uiste.com/20160826-1.html">
<meta property="og:site_name" content="uiste">
<meta property="og:description" content="复习：匿名函数use用法：将外部和匿名函数处于同一级的变量引用到匿名函数的内部，引用的变量和外部的变量是一个值传递的关系。如果需要引用传址，则在usb变量前加&amp;amp;符号

迭代
单单从思想的角度上看，递归和迭代没有谁优谁劣，都是一样的，就和大家偏文科偏理科一样，但是，从代码的角度上来看，无疑是迭代的思想更加具有效率，所以建议大家以后去多用迭代的思想去解决问题。

数组不是所有关联数组都是字符">
<meta property="og:updated_time" content="2016-08-26T14:39:27.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Day5-php数组相关">
<meta name="twitter:description" content="复习：匿名函数use用法：将外部和匿名函数处于同一级的变量引用到匿名函数的内部，引用的变量和外部的变量是一个值传递的关系。如果需要引用传址，则在usb变量前加&amp;amp;符号

迭代
单单从思想的角度上看，递归和迭代没有谁优谁劣，都是一样的，就和大家偏文科偏理科一样，但是，从代码的角度上来看，无疑是迭代的思想更加具有效率，所以建议大家以后去多用迭代的思想去解决问题。

数组不是所有关联数组都是字符">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 0,
      author: '博主'
    }
  };
</script>




  <link rel="canonical" href="http://blog.uiste.com/20160826-1.html"/>

  <title>
 Day5-php数组相关 | uiste 
</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  



  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?b58b87af012832b8ae35e33bb45b44fa";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>








  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">uiste</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">uiste个人博客</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      

            <li class="menu-item menu-item-search"> <a title="把这个链接拖到你的Chrome收藏夹工具栏中" href='javascript:(function() {
    function c() {
        var e = document.createElement("link");
        e.setAttribute("type", "text/css");
        e.setAttribute("rel", "stylesheet");
        e.setAttribute("href", f);
        e.setAttribute("class", l);
        document.body.appendChild(e)
    }
 
    function h() {
        var e = document.getElementsByClassName(l);
        for (var t = 0; t < e.length; t++) {
            document.body.removeChild(e[t])
        }
    }
 
    function p() {
        var e = document.createElement("div");
        e.setAttribute("class", a);
        document.body.appendChild(e);
        setTimeout(function() {
            document.body.removeChild(e)
        }, 100)
    }
 
    function d(e) {
        return {
            height : e.offsetHeight,
            width : e.offsetWidth
        }
    }
 
    function v(i) {
        var s = d(i);
        return s.height > e && s.height < n && s.width > t && s.width < r
    }
 
    function m(e) {
        var t = e;
        var n = 0;
        while (!!t) {
            n += t.offsetTop;
            t = t.offsetParent
        }
        return n
    }
 
    function g() {
        var e = document.documentElement;
        if (!!window.innerWidth) {
            return window.innerHeight
        } else if (e && !isNaN(e.clientHeight)) {
            return e.clientHeight
        }
        return 0
    }
 
    function y() {
        if (window.pageYOffset) {
            return window.pageYOffset
        }
        return Math.max(document.documentElement.scrollTop, document.body.scrollTop)
    }
 
    function E(e) {
        var t = m(e);
        return t >= w && t <= b + w
    }
 
    function S() {
        var e = document.createElement("audio");
        e.setAttribute("class", l);
        e.src = i;
        e.loop = false;
        e.addEventListener("canplay", function() {
            setTimeout(function() {
                x(k)
            }, 500);
            setTimeout(function() {
                N();
                p();
                for (var e = 0; e < O.length; e++) {
                    T(O[e])
                }
            }, 15500)
        }, true);
        e.addEventListener("ended", function() {
            N();
            h()
        }, true);
        e.innerHTML = " <p>If you are reading this, it is because your browser does not support the audio element. We recommend that you get a new browser.</p> <p>";
        document.body.appendChild(e);
        e.play()
    }
 
    function x(e) {
        e.className += " " + s + " " + o
    }
 
    function T(e) {
        e.className += " " + s + " " + u[Math.floor(Math.random() * u.length)]
    }
 
    function N() {
        var e = document.getElementsByClassName(s);
        var t = new RegExp("\\b" + s + "\\b");
        for (var n = 0; n < e.length; ) {
            e[n].className = e[n].className.replace(t, "")
        }
    }
 
    var e = 30;
    var t = 30;
    var n = 350;
    var r = 350;
    var i = "//s3.amazonaws.com/moovweb-marketing/playground/harlem-shake.mp3";
    var s = "mw-harlem_shake_me";
    var o = "im_first";
    var u = ["im_drunk", "im_baked", "im_trippin", "im_blown"];
    var a = "mw-strobe_light";
    var f = "//s3.amazonaws.com/moovweb-marketing/playground/harlem-shake-style.css";
    var l = "mw_added_css";
    var b = g();
    var w = y();
    var C = document.getElementsByTagName("*");
    var k = null;
    for (var L = 0; L < C.length; L++) {
        var A = C[L];
        if (v(A)) {
            if (E(A)) {
                k = A;
                break
            }
        }
    }
    if (A === null) {
        console.warn("Could not find a node of the right size. Please try a different page.");
        return
    }
    c();
    S();
    var O = [];
    for (var L = 0; L < C.length; L++) {
        var A = C[L];
        if (v(A)) {
            O.push(A)
        }
    }
})()    '>High</a> </li>

    </ul>
  

 
    <div class="site-search">
      
  <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>


    </div>
   
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Day5-php数组相关
              
            
          </h1>
        

        <div class="post-meta">
        
  <span id="busuanzi_container_page_pv">阅读量 <span id="busuanzi_value_page_pv"></span> 次</span>

          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2016-08-26T17:18:51+08:00" content="2016-08-26">
              2016-08-26
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/PHP/" itemprop="url" rel="index">
                    <span itemprop="name">PHP</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/20160826-1.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="20160826-1.html" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <blockquote>
<p>复习：<br>匿名函数use用法：将外部和匿名函数处于同一级的变量引用到匿名函数的内部，引用的变量和外部的变量是一个值传递的关系。如果需要引用传址，则在usb变量前加&amp;符号</p>
</blockquote>
<h3 id="迭代"><a href="#迭代" class="headerlink" title="迭代"></a>迭代</h3><blockquote>
<p>单单从思想的角度上看，递归和迭代没有谁优谁劣，都是一样的，就和大家偏文科偏理科一样，但是，从代码的角度上来看，无疑是迭代的思想更加具有效率，所以建议大家以后去多用迭代的思想去解决问题。</p>
</blockquote>
<h3 id="数组"><a href="#数组" class="headerlink" title="数组"></a>数组</h3><p>不是所有关联数组都是字符串下标<br><code>$arr = array(1110=&gt;&#39;刘德华&#39;,1111=&gt;&#39;金城武&#39;);</code><br>不是所有数组都是整形下标<br><code>$arr = array(&#39;a&#39;=&gt;&#39;雪碧&#39;,&#39;b&#39;=&gt;&#39;可乐&#39;,&#39;c&#39;=&gt;&#39;王老吉&#39;);</code></p>
<h4 id="foreach-遍历数组"><a href="#foreach-遍历数组" class="headerlink" title="foreach 遍历数组"></a>foreach 遍历数组</h4><p>语法：<br><code>foreach(需要遍历数组的名称 as 数组的键 =&gt; 数组的值){  }</code></p>
<p>语法：<br><code>foreach(需要遍历数组的名称 as  数组的值){  }</code></p>
<p>注意：</p>
<blockquote>
<p>foreach遍历数组的时候，数组的指针会自动的下移。<br>foreach遍历完数组以后，指针会指向一个非法的位置<br>foreach在遍历数组之前，会现对数组做一个初始化的操作</p>
</blockquote>
<p>foreach遍历数组，实际是遍历数组的拷贝值,改变原数组的结果，不会影响到遍历数组的结果,foreach遍历数组，如果改变$value的值，原数组不会发生变化</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">foreach($arr as $key =&gt; $value)&#123;</div><div class="line">	$value = &apos;加多宝&apos;;</div><div class="line">	echo &apos;数组的下标是：&apos;.$key.&apos;&lt;br&gt;&apos;;</div><div class="line">	echo &apos;数组的值为：&apos;.$value.&apos;&lt;br&gt;&apos;;</div><div class="line">&#125;</div><div class="line">var_dump($arr);</div></pre></td></tr></table></figure>
<p>foreach遍历数组同样支持引用传值，改变$value的值，原数组也会随之发生变化。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">foreach($arr as &amp;$value)&#123;</div><div class="line">	$value = &apos;加多宝&apos;;</div><div class="line">	echo &apos;数组的值为：&apos;.$value.&apos;&lt;br&gt;&apos;;</div><div class="line">&#125;</div><div class="line">var_dump($arr);</div></pre></td></tr></table></figure>
<blockquote>
<p>foreach遍历完数组以后，下标是一个不确定的情况，如果foreach遍历完以后需要使用数组，最好将他做一个初始化的操作。</p>
</blockquote>
<h4 id="指针-遍历数组"><a href="#指针-遍历数组" class="headerlink" title="指针 遍历数组"></a>指针 遍历数组</h4><blockquote>
<p>通过移动数组的指针，完成一个遍历数组的情况，指针指向的位置一般是分为一个合法位和非法位</p>
</blockquote>
<p>next($arr) :   让指针下移一位–&gt;指针下移<br>current($arr):  获取指针指向当前元素的值–&gt;获取当前值<br>key($arr)  :   获取指针指向当前元素的下标–&gt;获取当前元素下标</p>
<p>for(;current($arr);next($arr)){<br>    echo “数组元素的下标为：”.key($arr).’<br>‘;<br>    echo “数组元素的值为：”.current($arr).’<br>‘;<br>}</p>
<p>prev()        :    让数组的指针上移一位。</p>
<blockquote>
<p>上移指针和下移指针是相对于现在的位置而言，所以一旦指针处于一个非法位置，没有参照物了，指针无论上移还是下移都是非法位</p>
</blockquote>
<p>reset()   :   初始化指针。<br>end()    :   将数组指针指向数组的最后一个元素。</p>
<blockquote>
<p>reset()和end()理解成相当于一个绝对路径的概念，我不需要管数组指针当前指向的位置，直接将数组指针指向第一个元素或者最后一个元素</p>
</blockquote>
<p>each()  :返回一个四个元素的数组，其中1和value代表当前数组指向的值，0和key指向当前数组的下标。并且他会让数组的指针下移一位</p>
<h5 id="while-each-list-遍历数组"><a href="#while-each-list-遍历数组" class="headerlink" title="while+each+list 遍历数组"></a>while+each+list 遍历数组</h5><p>each可以获取元素的值，下标和下移指针，所以直接和while配合，就可以遍历数组<br><strong>方法一</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">while($element = each($arr))&#123;</div><div class="line">	echo &apos;下标：&apos;,$element[0];</div><div class="line">	echo &apos;值：&apos;,$element[1];</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p><strong>方法二</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">while(list($key,$value)=each($arr))&#123;</div><div class="line">	echo &apos;下标：&apos;,$key;</div><div class="line">	echo &apos;值：&apos;,$value;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>list（）    ：  list可以将数组里面的值赋值给一个变量</p>
<blockquote>
<p>each获取的是一个数组类型的变量，所以可以利用list将数组的值获取出来</p>
<p>3种遍历数组的方式，for(),foreach(),while+each+list的方式，以后工作中90%的人基本上用的都是foreach,9%都会使用while+each+list的方法，for基本上没有人使用</p>
</blockquote>
<h3 id="关于赋值的指针问题"><a href="#关于赋值的指针问题" class="headerlink" title="关于赋值的指针问题"></a>关于赋值的指针问题</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">$arr=array(‘a’,’b’,’c’,’d’);</div><div class="line">$arr2=$arr;</div><div class="line">echo current($arr2);</div><div class="line">//结果是 a，地址传递</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">$arr=array(‘a’,’b’,’c’,’d’);</div><div class="line">next($arr);</div><div class="line">$arr2=$arr;</div><div class="line">echo current($arr2);</div><div class="line">// 结果是b，复制数组的时候，指针跟着一起被复制</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">$arr=array(&apos;a&apos;,&apos;b&apos;,&apos;c&apos;,&apos;d&apos;);</div><div class="line">$arr2=$arr;</div><div class="line">next($arr);</div><div class="line">echo current($arr2);</div><div class="line">// 结果是a ,复制数组以后，修改一个数组的指针，另一个数组不会一起发生变化。</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">$arr=array(&apos;a&apos;,&apos;b&apos;,&apos;c&apos;,&apos;d&apos;);</div><div class="line">next($arr);</div><div class="line">next($arr);</div><div class="line">next($arr);</div><div class="line">next($arr);</div><div class="line">$arr2=$arr;</div><div class="line">echo current($arr2);</div><div class="line">// 结果是a ,当一个数组处于一个非法位置的时候，复制给另一个数组，此时先查看哪个，哪个就会被初始化，另一个就不会初始化；</div></pre></td></tr></table></figure>
<h3 id="COW-写时复制"><a href="#COW-写时复制" class="headerlink" title="COW 写时复制"></a>COW 写时复制</h3><p>如果将一个变量赋值给另一个变量，不会立刻去开辟一个空间，只有当变量进行写操作的时候，才会给他开辟一个空间。</p>
<p>memory_get_usage()    获取文件占用空间的大小。<br>array_fill（开始下标，添加元素的数量，默认值）<br>shuffle() - 将数组打乱<br>array_fill() - 用给定的值填充数组</p>
<p>在foreach里面对原数组进行写操作时，复制的数组才开辟了一块空间</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">	header(&quot;content-type:text/html;charset=utf-8&quot;);</div><div class="line">	$arr=array(&apos;可乐&apos;,&apos;雪碧&apos;,&apos;王老吉&apos;);</div><div class="line">	foreach($arr as $key =&gt; $value)&#123;</div><div class="line">	&#125;</div><div class="line">	var_dump(current($arr));</div><div class="line">//指针停留在非法位，没有进行写操作。</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">	header(&quot;content-type:text/html;charset=utf-8&quot;);</div><div class="line">	$arr=array(&apos;可乐&apos;,&apos;雪碧&apos;,&apos;王老吉&apos;);</div><div class="line">	foreach($arr as $key =&gt; $value)&#123;</div><div class="line">		$arr[1]=&apos;加多宝&apos;;</div><div class="line">	&#125;</div><div class="line">	var_dump(current($arr));</div><div class="line">//结果为加多宝，foreach循环的是复制的原始数组，里面更改的也是原始数组。</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">	header(&quot;content-type:text/html;charset=utf-8&quot;);</div><div class="line">	$arr=array(&apos;可乐&apos;,&apos;雪碧&apos;,&apos;王老吉&apos;);</div><div class="line">	foreach($arr as $key =&gt; $value)&#123;</div><div class="line">		if($key==1)&#123;</div><div class="line">			$arr[1]=&apos;加多宝&apos;;</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">	var_dump(current($arr));</div><div class="line">//结果为王老吉，进行了写操作。</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">	$arr=array(&apos;可乐&apos;,&apos;雪碧&apos;,&apos;王老吉&apos;);</div><div class="line">	foreach($arr as $key =&gt; $value)&#123;</div><div class="line">		if($key==2)&#123;</div><div class="line">			$arr[1]=&apos;加多宝&apos;;</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">	var_dump(current($arr));</div><div class="line">//结果为可乐，复制数组的时候，指针处于一个非法位置，所以指针被初始化操作了。</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">	header(&quot;content-type:text/html;charset=utf-8&quot;);</div><div class="line">	$arr=array(&apos;可乐&apos;,&apos;雪碧&apos;,&apos;王老吉&apos;);</div><div class="line">	foreach($arr as $key =&gt; $value)&#123;</div><div class="line">		if($key==3)&#123;</div><div class="line">			$arr[1]=&apos;加多宝&apos;;</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">	var_dump(current($arr));</div><div class="line">//if语句一直没有被执行，所以一直没有进行写时复制。所以遍历完数组以后，指针指向非法位</div></pre></td></tr></table></figure>
<p><strong>foreach变量数组总结</strong></p>
<blockquote>
<p>先复制后移动，原始数组保持指针不变<br>先移动后复制，复制的数组会将指针也复制了<br>指针处于非法位置时，进行复制，先查看哪个数组，哪个就先被初始化</p>
</blockquote>
<h3 id="关于数组的几个函数"><a href="#关于数组的几个函数" class="headerlink" title="关于数组的几个函数"></a>关于数组的几个函数</h3><p><strong>range（初始值，结束值）</strong><br><code>$arr = range(1,10);</code></p>
<p><strong>array_merge(数组1,数组2)</strong><br><code>$arr = array_merge(range(0,9),range(&#39;a&#39;,&#39;z&#39;));</code></p>
<blockquote>
<p>array_merge如果合并两个数字下标的数组，会将数字重新从0进行组合。<br>array_merge如果合并两个字符串下标的数组，后面的值会将前面的值覆盖。<br>array_merge合并两个数组，先从第一个数组开始合并，然后在合并第二个数组。</p>
</blockquote>
<p><strong>array_rand(数组，个数);</strong><br>随机获取一个数组的下标</p>
<blockquote>
<p>array_rand获取的是元素的下标，如果只获取一个元素下标，则是以普通数据类型进行存储，如果获取多个下标，则以数组的模式进行存储。<br>虽然array_rand是随机获取下标的，但是他获取多个下标的时候会从小到大排序</p>
</blockquote>
<p><strong>shuffle()</strong><br>可以打乱一个数组的元素</p>
<blockquote>
<p>返回值是bool(true),</p>
</blockquote>
<p><strong>随机验证码</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">$arr = array_merge(range(0,9),range(&apos;a&apos;,&apos;z&apos;),range(&apos;A&apos;,&apos;Z&apos;));</div><div class="line">$arr2 = array_rand($arr,4);</div><div class="line">shuffle($arr2);</div><div class="line">$str = &apos;&apos;;</div><div class="line">foreach ($arr2 as $value) &#123;</div><div class="line">	$str.=$arr[$value];</div><div class="line">&#125;</div><div class="line">var_dump($str);</div></pre></td></tr></table></figure>
<p>补充：</p>
<blockquote>
<p>memory_get_usage() 获取当前文件占用空间的大小；<br>array_fill(0,10,’uiste’); 0开始下标，10是元素个数，uiste是每个元素的内容</p>
</blockquote>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/PHP核心编程/" rel="tag">#PHP核心编程</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/20160824-1.html" rel="next" title="Day4-php函数基础">
                <i class="fa fa-chevron-left"></i> Day4-php函数基础
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/20160827-1.html" rel="prev" title="Day6-php数组、字符串">
                Day6-php数组、字符串 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        <!-- Go to www.addthis.com/dashboard to customize your tools -->
<script type = "text/javascript" src = "//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-57838eee5e2620d0" async = "async" ></script>
      
    </div>
  </div>


          </div>
          

  <p>热评文章</p>
  <div class="ds-top-threads" data-range="weekly" data-num-items="4"></div>


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="20160826-1.html"
           data-title="Day5-php数组相关" data-url="http://blog.uiste.com/20160826-1.html">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.png"
               alt="uiste" />
          <p class="site-author-name" itemprop="name">uiste</p>
          <p class="site-description motion-element" itemprop="description">Stay Hungry，Stay Foolish</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">70</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              
                <span class="site-state-item-count">13</span>
                <span class="site-state-item-name">分类</span>
              
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">55</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" rel="alternate">
              <i class="fa fa-rss"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/uiste" target="_blank" rel="external nofollow" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://weibo.com/uiste" target="_blank" rel="external nofollow" title="微博">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  微博
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://www.zhihu.com/people/uiste" target="_blank" rel="external nofollow" title="知乎">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  知乎
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              友情链接
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.uiste.com" title="uiste 个人博客" target="_blank">uiste 个人博客</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://uiste.github.io" title="uiste 技术博客" target="_blank">uiste 技术博客</a>
                </li>
              
            </ul>
          </div>
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#迭代"><span class="nav-number">1.</span> <span class="nav-text">迭代</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#数组"><span class="nav-number">2.</span> <span class="nav-text">数组</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#foreach-遍历数组"><span class="nav-number">2.1.</span> <span class="nav-text">foreach 遍历数组</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#指针-遍历数组"><span class="nav-number">2.2.</span> <span class="nav-text">指针 遍历数组</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#while-each-list-遍历数组"><span class="nav-number">2.2.1.</span> <span class="nav-text">while+each+list 遍历数组</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#关于赋值的指针问题"><span class="nav-number">3.</span> <span class="nav-text">关于赋值的指针问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#COW-写时复制"><span class="nav-number">4.</span> <span class="nav-text">COW 写时复制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#关于数组的几个函数"><span class="nav-number">5.</span> <span class="nav-text">关于数组的几个函数</span></a></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2015 - 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">uiste</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" href="http://hexo.io" rel="external nofollow">Hexo</a> 趋势动力
</div>
<!--站长统计-->
<script src="https://s95.cnzz.com/z_stat.php?id=1259894121&web_id=1259894121" language="JavaScript"></script>
<!--<div class="theme-info">
  Theme -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next" rel="external nofollow">
    NexT.Mist
  </a>
</div> -->
&nbsp;&nbsp;&nbsp;本站总点击 <span id="busuanzi_value_site_pv"></span> 次
&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;您是第 <span id="busuanzi_value_site_uv"></span> 位访客

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>

<script>
(function(){
    var bp = document.createElement('script');
    bp.src = '//push.zhanzhang.baidu.com/push.js';
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.1"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"uiste"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    <script src="/vendors/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  






  
  
  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length == 0) {
       search_path = "search.xml";
    }
    var path = "/" + search_path;
    // monitor main search box;

    function proceedsearch() {
      $("body").append('<div class="popoverlay">').css('overflow', 'hidden');
      $('.popup').toggle();

    }
    // search function;
    var searchFunc = function(path, search_id, content_id) {
    'use strict';
    $.ajax({
        url: path,
        dataType: "xml",
        async: true,
        success: function( xmlResponse ) {
            // get the contents from search data
            isfetched = true;
            $('.popup').detach().appendTo('.header-inner');
            var datas = $( "entry", xmlResponse ).map(function() {
                return {
                    title: $( "title", this ).text(),
                    content: $("content",this).text(),
                    url: $( "url" , this).text()
                };
            }).get();
            var $input = document.getElementById(search_id);
            var $resultContent = document.getElementById(content_id);
            $input.addEventListener('input', function(){
                var matchcounts = 0;
                var str='<ul class=\"search-result-list\">';
                var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                $resultContent.innerHTML = "";
                if (this.value.trim().length > 1) {
                // perform local searching
                datas.forEach(function(data) {
                    var isMatch = true;
                    var content_index = [];
                    var data_title = data.title.trim().toLowerCase();
                    var data_content = data.content.trim().replace(/<[^>]+>/g,"").toLowerCase();
                    var data_url = data.url;
                    var index_title = -1;
                    var index_content = -1;
                    var first_occur = -1;
                    // only match artiles with not empty titles and contents
                    if(data_title != '' && data_content != '') {
                        keywords.forEach(function(keyword, i) {
                            index_title = data_title.indexOf(keyword);
                            index_content = data_content.indexOf(keyword);
                            if( index_title < 0 && index_content < 0 ){
                                isMatch = false;
                            } else {
                                if (index_content < 0) {
                                    index_content = 0;
                                }
                                if (i == 0) {
                                    first_occur = index_content;
                                }
                            }
                        });
                    }
                    // show search results
                    if (isMatch) {
                        matchcounts += 1;
                        str += "<li><a href='"+ data_url +"' class='search-result-title'>"+ data_title +"</a>";
                        var content = data.content.trim().replace(/<[^>]+>/g,"");
                        if (first_occur >= 0) {
                            // cut out 100 characters
                            var start = first_occur - 20;
                            var end = first_occur + 80;
                            if(start < 0){
                                start = 0;
                            }
                            if(start == 0){
                                end = 50;
                            }
                            if(end > content.length){
                                end = content.length;
                            }
                            var match_content = content.substring(start, end);
                            // highlight all keywords
                            keywords.forEach(function(keyword){
                                var regS = new RegExp(keyword, "gi");
                                match_content = match_content.replace(regS, "<b class=\"search-keyword\">"+keyword+"</b>");
                            });

                            str += "<p class=\"search-result\">" + match_content +"...</p>"
                        }
                        str += "</li>";
                    }
                })};
                str += "</ul>";
                if (matchcounts == 0) { str = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>' }
                if (keywords == "") { str = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>' }
                $resultContent.innerHTML = str;
            });
            proceedsearch();
        }
    });}

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched == false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };

    });

    $('.popup-btn-close').click(function(e){
      $('.popup').hide();
      $(".popoverlay").remove();
      $('body').css('overflow', '');
    });
    $('.popup').click(function(e){
      e.stopPropagation();
    });
  </script>


  

  

  

</body>
</html>
